package benny.ApkposStore.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.util.Log;
import benny.ApkposStore.model.Zuoflist;
import benny.ApkposStore.utils.ApkConstant;
import benny.ApkposStore.utils.ApkUntils;

public class OrderZuoflist {
	/* table name */
	public static final String TABLENAME = "apkpos_orders_zuoflist";
	/* column names */
	static final public String CID = "_id"; // AUTO generate
	static final public String CTS = "cts"; // timestamp

	static final public String CORDERID = "orderid";
	static final public String DISH_ID = "dish_id";
	static final public String SUBDISH_ID = "subdish_id";
	static final public String ZUOF_ID = "zuof_id";
	static final public String DW = "dw";
	static final public String SL = "sl";
	public static final String JIAG = "jiag";
	public static final String JINE = "jine";
	public static final String MINGC = "mingc";

	private Context mCtx;

	public static final String createTableSQL = "create table if not exists "
			+ TABLENAME + "(" + CID + " integer primary key autoincrement,"
			+ CTS + " int not null default 0," + CORDERID
			+ " varchar(40)  not null default 0," + DISH_ID
			+ " int not null default 0," + SUBDISH_ID + " int," + ZUOF_ID
			+ "  int ," + DW + " varchar(10)," + SL + " numeric(18,2)," + JIAG
			+ "  numeric(18,2) ," + JINE + " numeric(18,2) ," + MINGC
			+ " varchar(10))";

	private static String tag = "apkpos_orders_zuoflist";

	public OrderZuoflist(Context ctx) {
		mCtx = ctx;
	}

	/**
	 * insert a row
	 * 
	 * @param bean
	 *            :
	 * @param context
	 *            :Application's context
	 * @return the row ID of the newly inserted row, or -1 if an error occurred
	 */
	public long insert(Zuoflist bean) {

		ContentValues values = beanToContentValues(bean);
		values.remove(CID);
		synchronized (ApkConstant.SQLLOCK) {
			try {
				SQLiteDatabase db = DBHelper.getWritableDatabase(mCtx);
				long rowId = db.insertOrThrow(TABLENAME, null, values);
				Log.d(tag, "insert a row success");
				return rowId;
			} catch (SQLiteException e) {
				Log.e(tag, "insert " + TABLENAME + " error:" + e.getMessage());
			}
		}

		return -1;
	}

	/**
	 * get a table cursor
	 * 
	 * @param ctx
	 *            :Application's context
	 * @return cursor
	 */
	public Cursor getCurSor(String selection, String selectionArgs[]) {
		String columns[] = { CID, CTS, CORDERID, DISH_ID, SUBDISH_ID, ZUOF_ID,
				DW, SL, JIAG, JINE, MINGC };
		// String selection = CORDERID +"= ?";
		// String selectionArgs[] = {"100"};
		String groupBy = "";
		String having = "";
		String orderBy = "";
		String limit = "";

		Cursor cursor = null;
		try {
			SQLiteDatabase db = DBHelper.getReadableDatabase(mCtx);
			cursor = db.query(TABLENAME, columns, selection, selectionArgs,
					groupBy, having, orderBy, limit);
			return cursor;
		} catch (SQLiteException e) {
			Log.e(tag, "query failed:" + e.getMessage());
		}
		return null;
	}

	/**
	 * update a service record
	 * 
	 * @param bean
	 */
	public boolean update(Zuoflist bean) {
		synchronized (ApkConstant.SQLLOCK) {
			try {
				String whereClause = " _id= " + bean.getId();
				SQLiteDatabase db = DBHelper.getWritableDatabase(mCtx);
				ContentValues values = beanToContentValues(bean);
				db.update(TABLENAME, values, whereClause, null);
				Log.d(tag, "update a row success");
			} catch (SQLiteException e) {
				Log.e(tag, "update " + TABLENAME + " error:" + e.getMessage());
				return false;
			}
		}
		return true;
	}

	/**
	 * delete a row
	 * 
	 * @param rowId
	 * @param ctx
	 * @return
	 */
	public boolean delete(int rowId) {
		try {

			String sql = "delete from " + TABLENAME + " where _id = " + rowId;
			ApkUntils.execSQL(mCtx, sql);
			Log.d(tag, "delete a row success");
		} catch (SQLiteException e) {
			Log.e(tag, "delete failed:" + e.getMessage());
			return false;
		}
		return true;
	}

	/**
	 * 按订单号删除
	 * 
	 * @param orderid
	 * @return
	 */
	public boolean deleteByOrderid(String orderid) {
		try {

			String sql = "delete from " + TABLENAME + " where " + CORDERID
					+ " = " + orderid;
			ApkUntils.execSQL(mCtx, sql);
			Log.d(tag, "delete a row success");
		} catch (SQLiteException e) {
			Log.e(tag, "delete failed:" + e.getMessage());
			return false;
		}
		return true;
	}

	/**
	 * convert db cursor to bean structure
	 * 
	 * @param cursor
	 * @return Order
	 */
	public Zuoflist CurSorToGasBean(Cursor cursor) {
		Zuoflist bean = new Zuoflist();
		bean.setId(cursor.getInt(cursor.getColumnIndex(CID)));
		bean.setTs(cursor.getInt(cursor.getColumnIndex(CTS)));
		bean.setOrderid(cursor.getString(cursor.getColumnIndex(CORDERID)));
		bean.setDish_id(cursor.getInt(cursor.getColumnIndex(DISH_ID)));
		bean.setSubdish_id(cursor.getInt(cursor.getColumnIndex(SUBDISH_ID)));
		bean.setZuof_id(cursor.getInt(cursor.getColumnIndex(ZUOF_ID)));
		bean.setDw(cursor.getString(cursor.getColumnIndex(DW)));
		bean.setSl(cursor.getFloat(cursor.getColumnIndex(SL)));
		bean.setJiag(cursor.getFloat(cursor.getColumnIndex(JIAG)));
		bean.setJine(cursor.getFloat(cursor.getColumnIndex(JINE)));
		bean.setMingc(cursor.getString(cursor.getColumnIndex(MINGC)));

		return bean;

	}

	private ContentValues beanToContentValues(Zuoflist bean) {
		ContentValues values = new ContentValues();

		values.put(CID, bean.getId());
		values.put(CTS, bean.getTs());
		values.put(CORDERID, bean.getOrderid());
		values.put(DISH_ID, bean.getDish_id());
		values.put(SUBDISH_ID, bean.getSubdish_id());
		values.put(ZUOF_ID, bean.getZuof_id());
		values.put(DW, bean.getDw());
		values.put(SL, bean.getSl());
		values.put(JIAG, bean.getJiag());
		values.put(JINE, bean.getJine());
		values.put(MINGC, bean.getMingc());

		return values;
	}

}
